<template>
  <div class="box-content">
    <Navbar :appTitle="title" :rtitle="rtitle" :rname="rname" />
    <Search ref="topSearch" class="search" placeholder="任务编号|设备编号|设备名称" :isAdd="true" :isScreen="true" :isType="true" @onSearch="onSearch" @onScreen="onScreen"
      @add="goAdd">
    </Search>
    <van-dropdown-menu active-color="#1989fa">
      <van-dropdown-item @change="comfirmType" v-model="params.taskStatus" :options="options1" />
      <van-dropdown-checkbox title="设备类别" :selectValue="selectedList2" :columns="options2" @comfirm="comfirmType2"></van-dropdown-checkbox>
    </van-dropdown-menu>
    <search-tag ref="SearchTag" :propsForSon="propsForSon" @getCategory="getCategory" v-show="showSearchTag"></search-tag>

    <!--分页列表 -->
    <div class="listBox" :style="{
        height: GLOBAL.currentEnvironment
          ? 'calc(100vh - 1rem)'
          : 'calc(100vh - 1.5rem)',
      }">
      <pageList ref="pageList" :checkedShow="checkedShow" :operationTxt="operationTxt" :columns="columns" :listUrl="listUrl" @getCheckedValue="getCheckedValue"
        @handleList="handleList">
        <div slot="head" slot-scope="{item}" class="title" style="width: 2.86rem; margin-left: 0.12rem">{{`${item.deviceCode}-${item.deviceName}`}}</div>
        <div class="rt-column" slot="body" slot-scope="{ item }">
          <div class="rt-row">
            <div class="column">
              <span class="column-t">任务编号：</span>
              <span class="column-c">{{ item.taskCode || '--' }}</span>
            </div>
          </div>
          <div class="rt-row" v-if="item.taskStatus == 1">
            <div class="column">
              <span class="column-t">设备类别：</span>
              <span class="column-c">{{ item.deviceCategoryText || '--' }}</span>
            </div>
          </div>
          <div class="rt-row" v-else>
            <div class="column">
              <span class="column-t">方案名称：</span>
              <span class="column-c">{{ item.programName || '--'}}</span>
            </div>
          </div>
        </div>
        <div slot="label" slot-scope="{ item }">
          <div class="label">
            <span :style="{ color: setColor(item) }">{{ item.taskStatus == 1 ? '已完成' : '待保养' }}</span>
            <span>{{ item.deviceCategoryText || '--' }}</span>
          </div>
        </div>
        <div slot="foot" slot-scope="{ item }" class="foot-slot" v-if="item.taskStatus == 0">
          <span></span><span @click.stop="toReport(item)">保养汇报</span>
        </div>
      </pageList>
    </div>
  </div>
</template>

<script>
import Search from "@/components/search";
import SearchTag from "./SearchTag";
import Navbar from "@/components/navBar/navBar_right";
import TabBar from "@/components/TabBar";
import pageList from "@/components/pageList";
import moment from "moment";
import VanDropdownCheckbox from "@/views/workManagement/defectsDeal/VanDropdownCheckbox";
import { getDictModel } from "@/api/system";
export default {
  name: "carRoomList",
  components: {
    Navbar,
    TabBar,
    pageList,
    Search,
    SearchTag,
    VanDropdownCheckbox,
  },
  data() {
    return {
      title: "保养任务",
      rtitle: "",
      rname: "", //search
      checkedShow: false, //列表是否支持选择
      operationTxt: "确定1", //选择按钮名称
      listUrl: "/mess/messDeviceMaintainTask/list", //列表接口
      columns: {
        content: [
          //内容
          {
            name: "任务编号",
            key: ["taskCode"],
          },
          {
            name: "设备类别",
            key: ["deviceCategoryText"],
          },
        ],
      },
      showSearchTag: false,
      keyword: "",
      params: {
        taskStatus: "0",
        taskStatusText: "待保养",
      },
      options1: [
        { text: "状态", value: "" },
        { text: "待保养", value: "0" },
        { text: "已完成", value: "1" },
      ],
      options2: [],
      selectedList: [],
      selectedList2: [],
      propsForSon: {},
    };
  },
  beforeRouteLeave(to, from, next) {
    let that = this;
    switch (true) {
      case that.showSearchTag && !that.$refs.SearchTag.ddthrVisible:
        that.showSearchTag = false;
        this.title = "保养任务";
        next(false);
        break;
      case that.$refs.SearchTag.ddthrVisible:
        that.$refs.SearchTag.ddthrVisible = false;
        this.title = "更多筛选";
        next(false);
        break;
      case !that.showSearchTag:
        next();
        break;
    }
  },
  computed: {},
  mounted() {
    this.searchQuery();
    this.getSelectList();
  },
  created() {},
  methods: {
    goAdd() {
      this.$router.push("/devicesCare/maintenanceTasksAdd");
    },
    // 标签颜色
    setColor(item) {
      let colors = {
        0: "#ff9d51",
        1: "#00b042",
      };
      return colors[item["taskStatus"]] || "";
    },
    getSelectList() {
      // 字典接口
      getDictModel({ dictCode: "mess_device_category" }).then((res) => {
        this.options2 = res.result;
        this.$refs.SearchTag.typeOptions = res.result;
      });
      getDictModel({ dictCode: "device_maintain_task_status" }).then((res) => {
        // let arr = res.result
        let arr = [{ text: "状态", value: "" }];
        arr = arr.concat(res.result);
        this.options1 = arr;
        this.$refs.SearchTag.statusOptions = res.result;
      });
    },
    comfirmType(v) {
      this.options1.forEach((item) => {
        if (item.value == v) {
          this.params.taskStatusText = v ? item.text : "";
        }
      });
      this.searchQuery();
    },
    comfirmType2(v) {
      this.params.deviceCategory = v;
      this.searchQuery();
    },
    /* 承诺交期是否超时判断 */
    promiseDateCheck(v) {
      let toDay = moment().format("YYYY-MM-DD");
      let checked = false;
      if (toDay == v) {
        checked = false;
      } else {
        checked = moment(v).isSameOrBefore(toDay);
      }
      return checked ? "" : "#ff4d4f";
    },
    toReport(v) {
      this.$router.push({
        path: "/devicesCare/maintenanceTasksReport",
        query: {
          id: v.id,
        },
      });
    },
    onSearch(v) {
      this.keyword = v;
      console.log(this.keyword);
      this.searchQuery();
    },
    onScreen() {
      this.keyword = this.$refs.topSearch.value;
      this.propsForSon = {
        ...this.params,
      };
      this.propsForSon.keyword = this.keyword;
      this.showSearchTag = true;
      this.title = "更多筛选";
    },
    resetCategory() {
      this.showSearchTag = false;
    },
    getCategory(p, k) {
      this.params = p;
      this.keyword = k;
      this.selectedList2 = p.deviceCategory ? p.deviceCategory.split(",") : [];
      this.showSearchTag = false;
      console.log(this.params);
      this.$refs.topSearch.value = k;
      this.title = "保养任务";
      this.searchQuery();
    },
    // 获取选择的列表数据
    getCheckedValue(ids, item) {
      console.log(ids, item);
    },
    //列表点击事件
    handleList(id, item, type) {
      if (type === "列表") {
        this.$router.push({
          path: "/devicesCare/maintenanceTasksDetail",
          query: {
            id: id,
          },
        });
      } else {
      }
    },
    // 加载列表
    getList() {
      // 传1代表从第一条数据开始
      this.$refs.pageList.onLoad(1);
    },
    // 搜索
    searchQuery() {
      // 搜索条件
      let queryParam = {
        ...this.params,
      };
      queryParam.keyword = this.keyword;
      // queryParam.sortColumn = 'taskCode';
      // queryParam.sortType = 'DESC';
      this.$refs.pageList.searchQuery(queryParam);
    },
    // 重置
    searchReset() {
      this.$refs.pageList.searchReset();
    },
    goSearch() {
      this.$router.push({ path: "/carRoomPlaneSearch" });
    },
  },
};
</script>
<style lang="scss" scoped>
.box-content {
  height: 100vh;
  overflow-y: hidden;
  .serach {
    height: 0.33rem;
    background: #f2f2f6;
    border-radius: 0rem;
    margin-bottom: 0.16rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    line-height: 0.33rem;
    padding: 0.1rem 0.12rem;
    // border: 1px solid red;
    .placeh {
      font-size: 0.16rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #888888;
      line-height: 0.23rem;
    }
    .icon {
      width: 0.15rem;
      height: 0.16rem;
    }
  }
  .serach-content {
    // height: 1.52rem;
    background: #ffffff;
    padding: 0 0.16rem 0.16rem 0.16rem;
    .content {
      display: flex;
      .tapbtn {
        // height: 0.23rem;
        // background: #1890ff;
        background: #f2f2f6;
        border-radius: 0.02rem;
        font-size: 0.16rem;
        font-family: PingFang SC;
        font-weight: 500;
        color: #888888;
        line-height: 0.23rem;
        margin-right: 0.12rem;
        text-align: center;
        padding: 0.04rem 0.28rem;
        cursor: pointer;
      }
      .active {
        color: #ffffff;
        background: #1890ff;
      }
    }
  }
}
.foot-slot {
  display: flex;
  justify-content: space-between;
  font-size: 0.14rem;
  color: #1890ff;
}
/deep/.van-nav-bar {
  z-index: 99 !important;
}
.rt-column {
  padding: 0.17rem 0 0 0.17rem;
  //  border: 1px solid red;
}
.rt-row {
  margin-bottom: 0.1rem;
  .column {
    width: 100%;
    .column-t {
      font-size: 0.14rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #888888;
      margin-bottom: 0.03rem;
    }
    .column-c {
      font-size: 0.14rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
    }
  }
}
.listBox {
  height: calc(100vh - 1.5rem);
  overflow-y: auto;
  .title {
    width: 3.1rem;
    margin-right: 0.1rem;
    // height: 0.16rem;
    font-size: 0.16rem;
    font-family: PingFang SC;
    font-weight: 500;
    color: #191f25;
    // line-height: 0.23rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .label {
    margin-left: -0.03rem;
    margin-top: -0.1rem;
    span {
      height: 0.16rem;
      background: #f0f6fe;
      color: #1890ff;
      margin-right: 0.1rem;
      padding: 0.03rem 0.08rem;
      font-size: 0.11rem;
      border-radius: 0.03rem;
    }
  }
}
</style>